package com.rzico.sbl.ui.statistic

import android.view.View
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.StockRecord
import com.rzico.sbl.other.LinearNormalDecoration
import com.rzico.sbl.ui.order.OrderDetailActivity
import com.rzico.sbl.viewmodel.StatisticStockViewModel
import com.xinnuo.common.extend.conditionFalse
import com.xinnuo.common.extend.conditionIf
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getColorEx
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.helper.dp2px
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.helper.intValue
import com.xinnuo.common.helper.sp2px
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class StockRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticStockViewModel>()

    override fun initData() {
        initTitle(title = "库存明细")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关记录信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        isLoadingMore
                            .conditionFalse {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                    }
                )

                view.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            "${(mList[pos] as StockRecord).letter}月"
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setHeaderHeight(dp2px(40f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<StockRecord>(R.layout.item_stock_record) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_record_memo, bean.memo.trimEnd('\n'))
                            text(R.id.item_record_time, bean.createDate.getTimeString())
                            text(R.id.item_record_sn, bean.sn.orEmpty())
                            text(
                                R.id.item_record_before,
                                (bean.balance.intValue + bean.debit.intValue - bean.credit.intValue).toString()
                            )
                            text(R.id.item_record_after, bean.balance)
                            text(R.id.item_record_name, bean.operator)

                            getView<View>(R.id.item_record_sn_ll)
                                .isGone = bean.sn.isNullOrEmpty()

                            with<TextView>(R.id.item_record_count) {
                                val credit = bean.credit.intValue //入库数
                                val debit = bean.debit.intValue   //出库数
                                val count = credit - debit

                                it.setTextColor(
                                    getColorEx(
                                        conditionIf(
                                            count > 0,
                                            R.color.black,
                                            R.color.red
                                        )
                                    )
                                )

                                it.text = when {
                                    count > 0 -> "+$count"
                                    count == 0 -> "-0"
                                    else -> count.toString()
                                }
                            }

                            clicked {
                                bean.sn.notEmpty {
                                    when {
                                        !bean.orderId.isNullOrEmpty() ->
                                            startActivityEx<OrderDetailActivity>(
                                                "orderSn" to bean.sn
                                            )

                                        !bean.creditId.isNullOrEmpty() ->
                                            startActivityEx<StockStoreDetailActivity>(
                                                "storeId" to bean.creditId
                                            )
                                    }
                                }
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    /**
     * 库存修改明细列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .goodsDetail(
                page = index,
                stockId = intent.getExtra("stockId"),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mList.onEach {
                        it as StockRecord
                        it.letter = TimeHelper
                            .getInstance()
                            .getMonthOfDate(
                                it.createDate.getTimeString(),
                                "yyyy-MM-dd HH:mm:ss"
                            )
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}